/*
 * @(#)LocalCollection.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.model.media;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.netexplode.jtunes.core.Configuration;
import com.netexplode.jtunes.core.ConfigurationAware;

/**
 * <code>LocalCollection</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.3 $
 * @since 0.1
 */
public class LocalCollection extends MediaCollection implements ConfigurationAware {

	private static final long serialVersionUID = 1L;

	private static final Log log = LogFactory.getLog(LocalCollection.class);

	private File device;

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	public synchronized File getDevice() {
		return this.device;
	}

	public synchronized void setDevice(File device) {
		this.device = device;
	}

	public void init() throws Exception {

		if (this.isDeviceActive()) {

			if (configuration.isThreaded()) {
				// loading up files is done in a seperate thread
				new Thread() {
					public void run() {
						try {
							find(device);
							log.info("adding " + mediaFiles.size() + " songs");
							// PlayerManager.getInstance().setInitialized(true);
						}
						catch (Exception ex) {
							log.warn(ex.toString(), ex);
						}
					}
				}.start();
			}
			else {
				this.find(device);
				log.info("adding " + mediaFiles.size() + " songs");
				// PlayerManager.getInstance().setInitialized(true);
			}
		}
	}

	private boolean isDeviceActive() {
		if (device == null) {
			log.warn("Null device");
			return false;
		}

		if (!device.exists()) {
			log.warn("device doesn't exist : " + device);
			return false;
		}

		if (!device.isDirectory()) {
			log.warn("device is not a directory : " + device);
			return false;
		}

		if (!device.canRead()) {
			log.warn("device is not readable  : " + device);
			return false;
		}
		return true;
	}

	private synchronized void find(File root) throws Exception {
		File[] files = root.listFiles();
		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()) {
				find(files[i]);
			}
			else {
				if (isSupportedMediaFile(files[i])) {
					Mp3File f = new Mp3File(files[i]);
					if (f.getTag() != null) {
						this.mediaFiles.add(f);
					}
					else {
						log.debug("no tag is set for " + f);
					}
				}
			}
		}
	}

	private boolean isSupportedMediaFile(File file) {
		// return (file.getName().endsWith("mp3") ||
		// file.getName().endsWith("mp4") || file.getName().endsWith("m4a"));
		return (file.getName().endsWith("mp3"));
	}
}